package main;

import javax.swing.*;
import storage.*;
import entity.*;
import java.awt.*;
import java.awt.event.*;

public class ChoosePrimaryKeyDialog extends JDialog
{	
	private JButton saveButton = new JButton("Save");
	private JButton cancelButton = new JButton("Cancel");
	private JComboBox columnInput = new JComboBox();
	private JPanel pane = new JPanel();
	private DataAccessManager data;
	private ManagementFrame parentFrame;
	private String databaseName;
	private String tableName;
	private Table table;
	
	Font fon = new Font("Dialog",Font.PLAIN,15);
	
	private String usr = "";
	private final JLabel lblColumn = new JLabel("Choose Column :");
	
	
	void buildConstraints(GridBagConstraints gbc,int gx, int gy, int gw, int gh, int wx, int wy)
	{
		gbc.gridx = gx;			
		gbc.gridy = gy;			
		gbc.gridwidth = gw;		
		gbc.gridheight = gh;	
		gbc.weightx = wx;		
		gbc.weighty = wy;		
	}
	
	
	private void setMyPane()
	{
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		pane.setLayout(gridbag);
		
		buildConstraints(constraints,0,0,3,1,1,1);
		
		buildConstraints(constraints,3,0,3,1,1,1);

		buildConstraints(constraints,0,2,2,1,1,1);

		buildConstraints(constraints,2,2,2,1,1,1);

		setLocation(200,80);
		setContentPane(pane);
		
		GridBagConstraints gbc_lblDataType = new GridBagConstraints();
		gbc_lblDataType.insets = new Insets(0, 0, 5, 5);
		gbc_lblDataType.gridx = 0;
		gbc_lblDataType.gridy = 0;
		pane.add(lblColumn, gbc_lblDataType);
		
		gridbag.setConstraints(columnInput, constraints);
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		model.addElement("None");
		for (int i=0;i<table.getColumnsCount();i++) model.addElement(table.getColumnAt(i).getName());
		columnInput.setModel(model);
		
		if (table.getPrimaryKey()!=-1) columnInput.setSelectedIndex(table.getPrimaryKey()+1);
		columnInput.setFont(fon);
		GridBagConstraints gbc_dbtypeInput = new GridBagConstraints();
		gbc_dbtypeInput.fill = GridBagConstraints.HORIZONTAL;
		gbc_dbtypeInput.insets = new Insets(0, 0, 5, 0);
		gbc_dbtypeInput.gridy = 0;
		gbc_dbtypeInput.gridx = 1;
		pane.add(columnInput, gbc_dbtypeInput);
		
		gridbag.setConstraints(saveButton, constraints);
		GridBagConstraints gbc_createButton = new GridBagConstraints();
		gbc_createButton.insets = new Insets(0, 0, 0, 5);
		gbc_createButton.gridx = 0;
		gbc_createButton.gridy = 2;
		pane.add(saveButton, gbc_createButton);
		
		gridbag.setConstraints(cancelButton, constraints);
		GridBagConstraints gbc_cancelButton = new GridBagConstraints();
		gbc_cancelButton.gridx = 1;
		gbc_cancelButton.gridy = 2;
		pane.add(cancelButton, gbc_cancelButton);
	}
	

	private void initListener()
	{
		ActListener actListener = new ActListener();
		saveButton.addActionListener(actListener);
		cancelButton.addActionListener(actListener);
	}
	
	class ActListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == saveButton)
				saveRun();
			if(e.getSource() == cancelButton)
				cancelRun();
		}
	}
	
	private void cancelRun()
	{
		this.dispose();
	}
	
	private void saveRun()
	{
		int temp = table.getPrimaryKey();
		String value = (String) columnInput.getSelectedItem();
		if (value.compareTo("None")==0) 
		{
			if (temp!=-1) table.getColumnAt(temp).setPrimaryKey(false);
			table.setPrimaryKey(-1);
		}
		else
		{
			if (temp!=-1) table.getColumnAt(temp).setPrimaryKey(false);
			table.setPrimaryKey(table.getColumnIndexByName(value));
			table.getColumnByName(value).setPrimaryKey(true);
		}
		data.saveDatabases();
		this.dispose();
	}
	public ChoosePrimaryKeyDialog(String databaseName , String tableName, ManagementFrame frame )
	{
		data = new DataAccessManager();
		parentFrame = frame;
		this.databaseName = databaseName;
		this.tableName = tableName;
		this.table = data.getDatabaseByName(databaseName).getTableByName(tableName);
		setTitle("Choose Primary Key - "+tableName);
		setModal(true);
		setSize(433,178);
		initListener();
		setMyPane();
		setVisible(true);
	}
}
